
package com.jf.cloud.distribution.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.platform.feign.ConfigFeignClient;
import com.jf.cloud.api.platform.vo.DistributionConfigApiVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.DistributionAudit;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.distribution.constant.EnterprisePayStatus;
import com.jf.cloud.distribution.dto.DistributionWithdrawCashDTO;
import com.jf.cloud.distribution.model.DistributionUser;
import com.jf.cloud.distribution.model.DistributionUserWallet;
import com.jf.cloud.distribution.model.DistributionWithdrawCash;
import com.jf.cloud.distribution.service.DistributionUserService;
import com.jf.cloud.distribution.service.DistributionUserWalletService;
import com.jf.cloud.distribution.service.DistributionWithdrawCashService;
import com.jf.cloud.distribution.vo.DistributionUserVO;
import com.jf.cloud.distribution.vo.DistributionWithdrawCashVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 分销员提现记录
 *
 * @author zz
 * @date 2021-08-09 14:14:10
 */
@RestController("platformDistributionWithdrawCashController")
@RequestMapping("/p/distribution_withdraw_cash")
@Tag(name = "分销员提现记录")
public class DistributionWithdrawCashController {

    @Autowired
    private DistributionWithdrawCashService distributionWithdrawCashService;
    @Autowired
    private DistributionUserWalletService distributionUserWalletService;
    @Autowired
    private DistributionUserService distributionUserService;
    @Autowired
    private ConfigFeignClient configFeignClient;
    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取分销员提现记录列表" , description = "分页获取分销员提现记录列表")
    public ServerResponseEntity<PageVO<DistributionWithdrawCashVO>> page(@Valid PageDTO pageDTO, String userMobile,@ParameterObject DistributionWithdrawCashDTO distributionWithdrawCashDTO) {
        PageVO<DistributionWithdrawCashVO> distributionWithdrawCashPage = distributionWithdrawCashService.page(pageDTO, userMobile, distributionWithdrawCashDTO);
        if (BooleanUtil.isFalse(permission)) {
            for (DistributionWithdrawCashVO record : distributionWithdrawCashPage.getList()) {
                DistributionUserVO distributionUser = record.getDistributionUserVO();
                if (Objects.nonNull(distributionUser)) {
                    if (StrUtil.isNotBlank(distributionUser.getNickName()) && PrincipalUtil.isMobile(distributionUser.getNickName())) {
                        distributionUser.setNickName(PhoneUtil.hideBetween(distributionUser.getNickName()).toString());
                    }
                    if (StrUtil.isNotBlank(distributionUser.getUserMobile())) {
                        distributionUser.setUserMobile(PhoneUtil.hideBetween(distributionUser.getUserMobile()).toString());
                    }
                }
            }
        }
        return ServerResponseEntity.success(distributionWithdrawCashPage);
    }


    @PutMapping("/to_success")
    @Operation(summary = "更新分销员提现记录" , description = "更新分销员提现记录")
    public ServerResponseEntity<Void> update(Long withdrawCashId) {
        DistributionWithdrawCash distributionWithdrawCash = distributionWithdrawCashService.getByWithdrawCashId(withdrawCashId);
        if (distributionWithdrawCash == null) {
            // 无法获取提现信息
            throw new LuckException("无法获取提现信息");
        }
        Long walletId = distributionWithdrawCash.getWalletId();
        DistributionUserWallet wallet = distributionUserWalletService.getByWalletId(walletId);
        if (wallet == null) {
            // 无法获取用户钱包信息
            throw new LuckException("无法获取用户钱包信息");
        }
        DistributionUser distributionUser = distributionUserService.getByDistributionUserId(wallet.getDistributionUserId());
        if (distributionUser == null) {
            // 无法获取用户信息
            throw new LuckException("无法获取用户信息");
        }
        distributionWithdrawCash.setState(1);
        distributionWithdrawCashService.update(distributionWithdrawCash);

        // 判断打款状态 1审核通过企业打款 2审核通过人工打款

        if (!distributionWithdrawCash.getMoneyFlow().equals(DistributionAudit.Withdrawals_ONE.value())){
            return ServerResponseEntity.success();
        }
        // 获取用户的openId
        DistributionUserWallet one = distributionUserWalletService.getByWalletId(distributionWithdrawCash.getWalletId());

        Long userId = distributionUserService.getByDistributionUserId(one.getDistributionUserId()).getUserId();
        // 添加企业支付记录
        distributionWithdrawCashService.enterprisePay(distributionWithdrawCash, userId, distributionWithdrawCash.getBizUserId(), EnterprisePayStatus.APPLY.getValue());
        return ServerResponseEntity.success();
    }

}
